import { IFeidaoAiMobile } from '@dfeidao/atom-mobile/interfaces';
import get from '@dfeidao/atom-mobile/local/get';
import remove from '@dfeidao/atom-mobile/local/remove';
import nodejs from '@dfeidao/atom-mobile/msg/nodejs';
import service from '@dfeidao/atom-mobile/msg/service';
import show_toast from '@dfeidao/atom-mobile/msgbox/toast';
import { spaceid } from '../atom/config';
import a001 from './a001';


function replacePos(strObj: string, pos: number, replacetext: string) {
	const str = strObj.substr(0, pos - 1) + replacetext + strObj.substring(pos, strObj.length);
	return str;
}

export default async function a006(fd: IFeidaoAiMobile) {
	const classes = get(fd, 'classes1') as { class_id: string, class_name: string };
	const subject = get(fd, 'subject1') as { subject_num: string, subject_name: string };
	const quote_job_name = get(fd, 'quote_job_name');
	const contents_homework = get(fd, 'contents_homework');
	if (!quote_job_name) {
		show_toast({
			type: 'danger',
			text: '请输入作业名称',
			position: 'center'
		});
		return;
	}

	if (!contents_homework) {
		show_toast({
			type: 'danger',
			text: '请输入作业内容',
			position: 'center'
		});
		return;
	}
	if (!classes) {
		show_toast({
			type: 'danger',
			text: '请选择班级',
			position: 'center'
		});
		return;
	}
	if (!subject) {
		show_toast({
			type: 'danger',
			text: '请选择科目',
			position: 'center'
		});
		return;
	}
	const class_id = classes.class_id;
	const class_name = classes.class_name;
	const subject_name = subject.subject_name;
	const subject_num = subject.subject_num;
	const teacher_no = get(fd, 'teacher_no');
	const teacher_name = get(fd, 'teacher_name');
	const productid = get(fd, 'productid');
	const ret = await nodejs<{ code: number }>('app-class-work/s002', { teacher_no, teacher_name, class_id, class_name, subject_num, subject_name, quote_job_name, contents_homework, productid });
	if (ret.code === 1) {
		show_toast({
			type: 'success',
			text: '发布成功',
			position: 'top'
		});
		remove(fd, 'classes1');
		remove(fd, 'subject1');
		remove(fd, 'quote_job_name');
		remove(fd, 'contents_homework');
		remove(fd, 'teacher_no');
		remove(fd, 'teacher_name');
		remove(fd, 'productid');
		await a001(fd);
		const openid_s = await nodejs<{ dataArray: Array<{ openid: string, student_name: string, parent_name: string }> }>('app-class-work/s003', { class_id, productid });
		const arr = new Array();
		if (openid_s.dataArray.length > 0) {
			openid_s.dataArray.forEach((i) => {
				arr.push({ student_name: i.student_name, parent_name: i.parent_name, openid: i.openid });
			});
		}
		if (openid_s.dataArray.length === 0) {
			show_toast({
				type: 'success',
				text: '发布成功',
				position: 'top'
			});
			return;
		}
		const information_template = await nodejs<Array<{ information_template_content: string }>>('app-class-work/s004', { productid });
		if (information_template.length === 0) {
			show_toast({
				type: 'success',
				text: '发布成功',
				position: 'top'
			});
			return;
		}
		const information_template_content = information_template[0].information_template_content;
		arr.forEach((v) => {
			const info = information_template_content;
			const i = info.indexOf('@', 0);
			const r = replacePos(info, i + 1, v.parent_name);
			const index = r.indexOf('@', 0);
			return v.information_template_content = replacePos(r, index + 1, v.student_name);
		});
		const d = await service("thirdpart.weixinpublicno", {
			"serviceflag": "getaccesstoken",      // 固定参数
			"spaceid": spaceid,           // 当前项目的spaceid
			"thirdpay_type": "weixin_pubno", // 固定参数（weixin_pubno）
			"productid": productid    // 当前登录用户对应的productid（从系统参数params中获取）
		}) as { access_token: string };
		arr.forEach(async (i) => {
			try {
				const result = await service('thirdpart.weixinpubno.bulkpushmessage', {
					"tousers": [
						i.openid/// res1中的openid
					],
					"spaceid": spaceid,
					"productid": productid,
					"access_token": d.access_token, /// 获取的token
					"custombody": {
						"msgtype": 'text', /// 推送的消
						'text': {
							'content': i.information_template_content
						}
					}
				}) as { code: number, msg: string };
				if (result.code > 0) {
					show_toast({
						type: 'success',
						text: '发布成功',
						position: 'top'
					});
					return;
				}
			} catch (error) {
				show_toast({
					type: 'success',
					text: '发布成功',
					position: 'top'
				});
				return;
			}
		});
	} else {
		await show_toast({
			type: 'danger',
			text: '发布失败，请稍后重试！',
			position: 'top'
		});
		return;
	}
}
